package service.impl;

import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;

import model.EstpInfo;
import model.ProjectInfo;
import model.ProspectusInfo;
import model.User;
import service.ProspectusInfoManager;
import dao.ProspectusInfoDao;

public class ProspectusInfoManagerImpl implements ProspectusInfoManager {
	private ProspectusInfoDao prospectusInfoDao;
	public ProspectusInfoDao getProspectusInfoDao() {
		return prospectusInfoDao;
	}
	public void setProspectusInfoDao(ProspectusInfoDao prospectusInfoDao) {
		this.prospectusInfoDao = prospectusInfoDao;
	}
	public int addProspectusInfoManager(String prosPerson, String prosProName,
			String proslinkman, String proslinkPhone, String prosEmail,
			String prosAddress, String prosPostcode, String prosFax)
			throws Exception {
		// TODO Auto-generated method stub
		try {
			ProspectusInfo pi = new ProspectusInfo();
			pi.setProsPerson(prosPerson);
			pi.setProsProName(prosProName);
			pi.setProslinkman(proslinkman);
			pi.setProslinkPhone(proslinkPhone);
			pi.setProsEmail(prosEmail);
			pi.setProsAddress(prosAddress);
			pi.setProsPostcode(prosPostcode);
			pi.setProsFax(prosFax);
			Map session = ActionContext.getContext().getSession();
			User user = (User)session.get("USER");
			pi.setUser(user);
			prospectusInfoDao.save(pi);
			return pi.getId();
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("新增计划书信息时出现异常");
			// TODO: handle exception
		}
	}
	
	public int addOrUpdateProspectusInfoManager(String prosPerson, String prosProName,
			String proslinkman, String proslinkPhone, String prosEmail,
			String prosAddress, String prosPostcode, String prosFax)
			throws Exception {
		// TODO Auto-generated method stub
		try {
			Map session = ActionContext.getContext().getSession();
			ProspectusInfo pi = (ProspectusInfo)session.get("PI");
			if(pi == null){
				pi = new ProspectusInfo();
			}
			pi.setProsPerson(prosPerson);
			pi.setProsProName(prosProName);
			pi.setProslinkman(proslinkman);
			pi.setProslinkPhone(proslinkPhone);
			pi.setProsEmail(prosEmail);
			pi.setProsAddress(prosAddress);
			pi.setProsPostcode(prosPostcode);
			pi.setProsFax(prosFax);
			User user = (User)session.get("USER");
			pi.setUser(user);
			prospectusInfoDao.saveOrUpdate(pi);
			return pi.getId();
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("新增或更新计划书信息时出现异常");
			// TODO: handle exception
		}
	}
	
	public List<ProspectusInfo> getProspectusByUser(User user) throws Exception{
		try {
			List<ProspectusInfo> pInfo = prospectusInfoDao.getProspectusByUser(user);
			return pInfo;
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			throw new Exception("按用户查找计划书信息时出现异常");
		}
	}

}
